# connect-file-cache

A static cache middleware for [Connect](http://senchalabs.github.com/connect/) spun off from [connect-assets](http://github.com/TrevorBurnham/connect-assets). Written in [CoffeeScript](http://coffeescript.org) by the author of [CoffeeScript: Accelerated JavaScript Development](http://pragprog.com/book/tbcoffee/coffeescript).

## Why connect-file-cache?

If you're just serving files, Connect's own [static](http://senchalabs.github.com/connect/middleware-static.html) (possibly with [staticCache](http://senchalabs.github.com/connect/middleware-staticCache.html) on top) is quite sufficient. But suppose you want to serve, say, compiled and minified CoffeeScript with a far-future expires header, and you want multiple routes to be able to point at that same file (say, one with an MD5 suffix for disambiguation, and one without).

That's the use case connect-file-cache was born out of. It's designed to be very fast. The "files" it serves don't have to exist on disk at all. On the other hand, it's not suitable for serving large files (or many), because *all files are stored in memory.*

**Bonus:** connect-file-cache also does gzip (when requested)!

## How do I use it?

Install:

    npm install connect-file-cache

Then in your app:

    connectCache = require 'connect-file-cache'
    cache = connectCache options
    app.use cache.middleware

where `options` is a hash that may include

* `src`: A dir containing files to be served directly (defaults to `null`)
* `routePrefix`: Data will be served from this path (defaults to `/`)

You can map routes directly to strings like so:

    cache.set 'foo/bar.txt', 'str'

Then go to `yourapp/foo/bar.txt` in your browser.

## Additional Features

You can map multiple routes by passing in an array as the first argument:

    cache.set ['foo/bar.txt', 'foo/baz.txt'], 'str'

And if you want the browser to download the file rather than trying to display it, set the `attachment` flag to `true`:

    cache.set 'foo/bar.txt', 'str', attachment: true

By default, MIME types are automatically inferred from file extensions. But you can override this with the `mime` option:

    cache.set 'foo/bar.txt', 'str', mime: 'html'

To support browser-side caching, connect-file-cache handles "If-Modified-Since" requests. But to prevent even those few milliseconds of extra latency, you can set `expires` to `false`—meaning that the browser will never download the file again (from the same route):

    cache.set 'foo/bar.txt', 'str', expires: false

For more advanced examples, check the `test` folder.

## Credits

Thoroughly inspired by Connect's [static](https://github.com/senchalabs/connect/blob/1.6.4/lib/middleware/static.js) middleware.

## License

©2011 Trevor Burnham and available under the [MIT license](http://www.opensource.org/licenses/mit-license.php):

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.